package com.htdata.iiot.tools;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

public class Profile {

    public static Profile profile = new Profile();

    private String   jarAbsPath        = null;
    private String   propConfFile      = null;
    private boolean   snappy          = false;
    private boolean   zlib          = false;
    private boolean   lzo          = false;
    private boolean   lz4          = false;
    private boolean   gzip          = false;
    private boolean   noCompress    = false;
    


    private Properties properties = new Properties();



    public Profile() {
        URI uri = null;
        File file = null;


        try {
            uri = Profile.class.getClassLoader().getResource("").toURI();
            file = new File(uri);
            if (file.isFile() || file.getPath().endsWith(".jar") || (".zip").equals(file.getPath())) {
                jarAbsPath = file.getParent();
            } else {
                jarAbsPath = file.getPath();
            }
            System.setProperty("jarAbsPath", jarAbsPath);


            propConfFile = Paths.get(jarAbsPath, "conf", "compress.properties").toString();
            loadContent();



        } catch (Exception e) {

            
        }
    }


  

    private void loadContent() throws Exception {
        File file = new File(propConfFile);
        FileInputStream fStream = new FileInputStream(file);
        InputStreamReader inStream = new InputStreamReader(fStream, "UTF-8");
        BufferedReader bReader = new BufferedReader(inStream);

        properties.clear();
        properties.load(bReader);

        String sy = properties.getProperty("compress.snappy", "0");
        if (null == sy || sy.isEmpty()) {
            throw new ExceptionInInitializerError("compress.snappy in '" + propConfFile + "' must not be empty!");
        } else {
            sy = sy.trim();
            if(sy.equals("1")){
            	snappy = true;
            }else{
            	snappy = false;
            }

        }
        
        String lo = properties.getProperty("compress.lzo", "0");
        if (null == lo || lo.isEmpty()) {
            throw new ExceptionInInitializerError("compress.lzo in '" + propConfFile + "' must not be empty!");
        } else {
        	lo = lo.trim();
            if(lo.equals("1")){
            	lzo = true;
            }else{
            	lzo = false;
            }

        }
        
        String noC = properties.getProperty("compress.nocompress", "0");
        if (null == noC || noC.isEmpty()) {
            throw new ExceptionInInitializerError("compress.nocompress in '" + propConfFile + "' must not be empty!");
        } else {
        	noC = noC.trim();
            if(noC.equals("1")){
            	noCompress = true;
            }else{
            	noCompress = false;
            }

        }
        
        String lz = properties.getProperty("compress.lz4", "0");
        if (null == lz || lz.isEmpty()) {
            throw new ExceptionInInitializerError("compress.lz4 in '" + propConfFile + "' must not be empty!");
        } else {
        	lz = lz.trim();
            if(lz.equals("1")){
            	lz4 = true;
            }else{
            	lz4 = false;
            }

        }
        String zb = properties.getProperty("compress.zlib", "0");
        if (null == zb || zb.isEmpty()) {
            throw new ExceptionInInitializerError("compress.zlib in '" + propConfFile + "' must not be empty!");
        } else {
            zb = zb.trim();
            if(zb.equals("1")){
            	zlib = true;
            }else{
            	zlib = false;
            }

        }
        String gp = properties.getProperty("compress.gzip", "0");
        if (null == gp || gp.isEmpty()) {
            throw new ExceptionInInitializerError("compress.gzip in '" + propConfFile + "' must not be empty!");
        } else {
        	gp = gp.trim();
            if(gp.equals("1")){
            	zlib = true;
            }else{
            	zlib = false;
            }

        }


     
        fStream.close();
        inStream.close();
    }




	public boolean isSnappy() {
		return snappy;
	}




	public void setSnappy(boolean snappy) {
		this.snappy = snappy;
	}




	public boolean isZlib() {
		return zlib;
	}




	public void setZlib(boolean zlib) {
		this.zlib = zlib;
	}




	public boolean isLzo() {
		return lzo;
	}




	public void setLzo(boolean lzo) {
		this.lzo = lzo;
	}




	public boolean isGzip() {
		return gzip;
	}




	public void setGzip(boolean gzip) {
		this.gzip = gzip;
	}

	public boolean isLz4() {
		return lz4;
	}




	public void setLz4(boolean lz4) {
		this.lz4 = lz4;
	}

	public boolean isNoCompress() {
		return noCompress;
	}

	public void setNoCompress(boolean noCompress) {
		this.noCompress = noCompress;
	}
    
}
